<template>
  <div>
    <el-dialog title="信号定义" v-model="dialogVisible" width="700px" :close-on-click-modal="false"
      :close-on-press-escape="false" @closed="$emit('close')">
      <el-button type="primary" size="small" @click="addRow">新增</el-button>
      <el-form ref="form" :model="formData" :rules="rules">
        <el-table ref="tab" :data="formData.signal">
          <el-table-column label="scope" width="180">
            <template #default="scope">
              <el-form-item :prop="'signal.' + scope.$index + '.scope'" :rules="rules.scope">
                <el-select v-model="scope.row.scope" placeholder="scope">
                  <el-option v-for="item in scopes" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="id" width="180">
            <template #default="scope">
              <el-form-item :prop="'signal.' + scope.$index + '.id'" :rules="rules.id">
                <el-input v-model="scope.row.id" placeholder="id" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="名称" width="180">
            <template #default="scope">
              <el-form-item :prop="'signal.' + scope.$index + '.name'" :rules="rules.name">
                <el-input v-model="scope.row.name" placeholder="名称" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100">
            <template #default="scope">
              <el-button @click="delRow(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="closeDialog">确 定 </el-button>
        </span>
      </template>

    </el-dialog>
  </div>
</template>

<script>
import mixinPanel from '../../../common/mixinPanel'
export default {
  mixins: [mixinPanel],
  data() {
    return {
      dialogVisible: true,
      formData: {
        signal: []
      },
      scopes: [
        { label: '全局', value: 'start' },
        { label: '流程实例', value: 'end' }
      ],
      rules: {
        scope: [
          { required: true, message: '请选择', trigger: ['blur', 'change'] },
        ],
        id: [
          { required: true, message: '请输入', trigger: ['blur', 'change'] },
        ],
        name: [
          { required: true, message: '请输入', trigger: ['blur', 'change'] },
        ],
      },
    }
  },
  computed: {
  },
  mounted() {
    this.formData.signal = this.element.businessObject.extensionElements?.values.map(item => {
      return {
        id: item.id,
        name: item.name,
        scope: item.scope
      }
    }) ?? []
  },
  methods: {
    updateElement() {
      if (this.formData.signal?.length) {
        let extensionElements = this.element.businessObject.get('extensionElements')
        if (!extensionElements) {
          extensionElements = this.modeler.get('moddle').create('bpmn:ExtensionElements')
        }
        // 清除旧值
        extensionElements.values = extensionElements.values?.filter(item => item.$type !== 'flowable:ExecutionListener') ?? []
        this.formData.signal.forEach(item => {
          const executionListener = this.modeler.get('moddle').create('flowable:ExecutionListener')
          executionListener['id'] = item.id
          executionListener['name'] = item.name
          executionListener['scope'] = item.scope
          extensionElements.get('values').push(executionListener)
        })
        this.updateProperties({ extensionElements: extensionElements })
      } else {
        let extensionElements = this.element.businessObject.get('extensionElements')
        if (extensionElements) {
          extensionElements.values = extensionElements.values?.filter(item => item.$type !== 'flowable:ExecutionListener') ?? []
        }
      }
    },
    closeDialog() {
      this.$refs.form.validate().then(() => {
        this.updateElement()
        this.dialogVisible = false
      }).catch(e => console.error(e))
    },
    addRow() {
      this.formData.signal.push({})
    },
    delRow(idx) {
      this.formData.signal.splice(idx, 1)
    }
  }
}
</script>

<style>
.flow-containers .el-badge__content.is-fixed {
  top: 18px;
}
</style>
